package drive.Mapper;

import drive.Model.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {
    // 查询所有评论
    @Select("SELECT * FROM comment")
    List<Comment> findAll();

    // 根据commentID查询评论
    @Select("SELECT * FROM comment WHERE commentID = #{commentID}")
    Comment findByCommentID(int commentID);

    @Delete("Delete FROM comment WHERE parentCommentID = #{parentCommentID}")
    int delete_son_comment(int parentCommentID);

    @Delete("Delete FROM comment WHERE articleID = #{articleID}")
    int delete_article_comment(int articleID);

    // 根据articleID查询评论
    @Select("SELECT * FROM comment WHERE articleID = #{articleID}")
    List<Comment> findByArticleID(int articleID);

    // 根据userID查询评论
    @Select("SELECT * FROM comment WHERE userID = #{userID}")
    List<Comment> findByUserID(int userID);

    // 根据parentCommentID查询评论
    @Select("SELECT * FROM comment WHERE parentCommentID = #{parentCommentID}")
    List<Comment> findByParentCommentID(int parentCommentID);

    // 新增评论
    @Insert("INSERT INTO comment (articleID, userID, parentCommentID, content, creationTime) VALUES (#{articleID}, #{userID}, #{parentCommentID}, #{content}, #{creationTime})")
    @Options(useGeneratedKeys = true, keyProperty = "commentID")
    int add(Comment comment);


    @Insert("INSERT INTO comment (articleID, userID, parentCommentID, content, creationTime) VALUES (#{articleID}, #{userID}, null, #{content}, #{creationTime})")
    @Options(useGeneratedKeys = true, keyProperty = "commentID")
    int add_top(Comment comment);
    // 更新评论
    @Update("UPDATE comment SET articleID = #{articleID}, userID = #{userID}, content = #{content}, creationTime = #{creationTime} WHERE commentID = #{commentID}")
    int update(Comment comment);

    // 删除评论
    @Delete("DELETE FROM comment WHERE commentID = #{commentID}")
    int delete(int commentID);

    /**
     * 根据文章ID统计评论数量
     * @param articleId 文章ID
     * @return 评论数量
     */
    @Select("SELECT COUNT(*) FROM comment WHERE articleID = #{articleId}")
    int countByArticleId(int articleId);
}
